﻿using System;
using System.Web.UI.Design;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI;
using System.Text;

namespace TPG.SharePoint.Workflow.Controls.Design
{
    [SupportsPreviewControl(true)]
    public class InputFormSectionDesigner
        : ControlDesigner
    {
        protected InputFormSection Section
        {
            get { return (InputFormSection)Component; }
        }

        protected InputFormSection ViewSection
        {
            get { return (InputFormSection)ViewControl; }
        }

        protected override bool UsePreviewControl
        {
            get { return true; }
        }

        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                TemplateGroupCollection groups = base.TemplateGroups;
                TemplateGroup headingsGroup = new TemplateGroup("Headings");
                headingsGroup.AddTemplateDefinition(
                    new TemplateDefinition(this, "TitleTemplate", Component, "TitleTemplate"));
                headingsGroup.AddTemplateDefinition(
                    new TemplateDefinition(this, "DescriptionTemplate", Component, "DescriptionTemplate"));
                groups.Add(headingsGroup);
                TemplateGroup contentGroup = new TemplateGroup("Content");
                contentGroup.AddTemplateDefinition(
                    new TemplateDefinition(this, "ContentTemplate", Component, "ContentTemplate"));
                groups.Add(contentGroup);
                return groups;
            }
        }

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection lists = base.ActionLists;
                lists.Add(new InputFormSectionActionList(this));
                return lists;
            }
        }

        public override void Initialize(IComponent component)
        {
            if (component is InputFormSection == false)
            {
                throw new ArgumentException();
            }
            base.Initialize(component);
            SetViewFlags(ViewFlags.TemplateEditing, true);
        }

        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            regions.Add(CreateTemplateRegion("ContentTemplate"));
            if (Section.TitleTemplate != null)
            {
                regions.Add(CreateTemplateRegion("TitleTemplate"));
            }
            if (Section.DescriptionTemplate != null)
            {
                regions.Add(CreateTemplateRegion("DescriptionTemplate"));
            }
            return GetDesignTimeHtml();
        }

        void AddRegionAttribute(WebControl control, int regionID)
        {
            control.Controls.Clear();
            control.Attributes.Add(DesignerRegion.DesignerRegionAttributeName, regionID.ToString());
        }

        public override string GetDesignTimeHtml()
        {
            ((ICompositeControlDesignerAccessor)ViewSection).RecreateChildControls();
            IDictionary state = ((IControlDesignerAccessor)ViewSection).GetDesignModeState();

            int regionID = 0;
            AddRegionAttribute((WebControl)state["ContentTemplateContainer"], regionID++);

            if (Section.TitleTemplate != null)
            {
                AddRegionAttribute((WebControl)state["TitleTemplateContainer"], regionID++);
            }
            if (Section.DescriptionTemplate != null)
            {
                AddRegionAttribute((WebControl)state["DescriptionTemplateContainer"], regionID++);
            }
            SPDesignTimeStyleManager.ApplySharePointStyleRecursive(ViewSection);
            return base.GetDesignTimeHtml();
        }

        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            return DesignerRegionHelper.GetTemplatedEditableDesignerRegionContent(
                host, (TemplatedEditableDesignerRegion)region);
        }

        public override void SetEditableDesignerRegionContent(
            EditableDesignerRegion region, string content)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerRegionHelper.SetTemplatedEditableDesignerRegionContent(
                host, (TemplatedEditableDesignerRegion)region, content);
            ViewControlCreated = false;
        }

        internal void ConvertToTitleTemplate()
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerRegionHelper.ConvertToTemplate(
                host, "TitleTemplate", Section, "TitleTemplateContainer");
            ViewControlCreated = false;
        }

        internal void ResetTitleTemplate()
        {
            DesignerRegionHelper.ResetTemplate("TitleTemplate", Section);
        }

        internal void ConvertToDescriptionTemplate()
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerRegionHelper.ConvertToTemplate(
                host, "DescriptionTemplate", Section, "DescriptionTemplateContainer");
            ViewControlCreated = false;
        }

        internal void ResetDescriptionTemplate()
        {
            DesignerRegionHelper.ResetTemplate("DescriptionTemplate", Section);
        }

        DesignerRegion CreateTemplateRegion(string regionName)
        {
            TemplateDefinition template = new TemplateDefinition(
                    this, regionName, Section, regionName);
            TemplatedEditableDesignerRegion region =
                new TemplatedEditableDesignerRegion(template);
            region.IsSingleInstanceTemplate = true;
            return region;
        }

        class InputFormSectionActionList
            : DesignerActionList
        {
            InputFormSectionDesigner _designer;

            public InputFormSectionActionList(InputFormSectionDesigner designer)
                : base(designer.Component)
            {
                _designer = designer;
            }

            public override DesignerActionItemCollection GetSortedActionItems()
            {
                DesignerActionItemCollection items = new DesignerActionItemCollection();
                if (_designer.InTemplateMode == false)
                {
                    if (_designer.Section.TitleTemplate == null)
                    {
                        items.Add(new DesignerActionMethodItem(
                            this, "ConvertToTitleTemplate", "Convert to title template", true));
                    }
                    else
                    {
                        items.Add(new DesignerActionMethodItem(
                            this, "ResetTitleTemplate", "Reset title template", true));
                    }
                    if (_designer.Section.DescriptionTemplate == null)
                    {
                        items.Add(new DesignerActionMethodItem(
                            this, "ConvertToDescriptionTemplate", "Convert to description template", true));
                    }
                    else
                    {
                        items.Add(new DesignerActionMethodItem(
                            this, "ResetDescriptionTemplate", "Reset description template", true));
                    }
                }
#if DEBUG
                items.Add(new DesignerActionMethodItem(
                    this, "GetHtml", "Get Html"));
#endif
                return items;
            }
#if DEBUG
            public void GetHtml()
            {
                HtmlPreviewer.ShowForDesigner(_designer);
            }
#endif

            public void ConvertToTitleTemplate()
            {
                _designer.ConvertToTitleTemplate();
            }

            public void ResetTitleTemplate()
            {
                _designer.ResetTitleTemplate();
            }

            public void ConvertToDescriptionTemplate()
            {
                _designer.ConvertToDescriptionTemplate();
            }

            public void ResetDescriptionTemplate()
            {
                _designer.ResetDescriptionTemplate();
            }
        }
    }
}
